import axios from "axios";
// const baseURL = {
//   development: "http://localhost:3000",
//   production: "http://api.example.com",
// };

const instance = axios.create({
  baseURL: import.meta.env.VITE_APP_BASE_URL,
});

const whileURLs=['/login']

instance.interceptors.request.use(
  (config) => {
    const token = localStorage.getItem('token');;
    let url=config.url;
    if(whileURLs.indexOf(url)>-1){
        return config;
    }
    if(token){
          config.headers.Authorization = ` Bearer ${token}`;
    }else{
      window.location.href='/login'
      return;
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

instance.interceptors.response.use(
  (response) => {
    return response.data;
  },
  (error) => {
    if (error.response) {
      switch (error.response.status) {
        case 400:
          break;
        case 401:
          break;
        case 404:
          break;
        default:
          break;
      }
    }else if(error.request){
        console.log('网络异常，请稍后重试');
    }else{
        console.log('请求失败',error,message);
    }
    return Promise.reject(error)
  }
);

export default instance
